import 'package:flutter/material.dart';
enum EmptyStatus {
  ///加载中
  LOADING,

  ///有网络无数据
  CONNECT,

  ///无网络或弱网
  DISCONNECT
}

class EmptyWidget extends StatelessWidget {
  final Function onTap;
  final EmptyStatus status;
  final String statusText;
  final String statusDetailText;
  final Color backgroundColor;

  EmptyWidget(
      {Key key,
      this.onTap,
      this.status,
      this.statusText,
      this.statusDetailText,
      this.backgroundColor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // ScreenUtil.init(context, width: 375, height: 812);
    return Scaffold(
        body: Container(
      color: backgroundColor == null ? null : backgroundColor,
      width: MediaQuery.of(context).size.width,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset("assets/images/empty_placeholder_img.png",
              width: 99, height: 88),
          Padding(
            padding: EdgeInsets.only(top: 15),
            child: Text(
              _getStatusText(),
              style: TextStyle(
                color: Color(0xFF333333),
                fontSize: 16,
              ),
            ),
          ),
          Offstage(
            offstage:
                statusDetailText == null && status != EmptyStatus.DISCONNECT,
            child: Padding(
              padding:
                  EdgeInsets.only(top: 10, left: 75, right: 75),
              child: Text(
                _getStatusDetailText(),
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Color(0xFF999999),
                  fontSize: 12,
                ),
              ),
            ),
          ),
          Offstage(
              offstage: onTap == null,
              child: Padding(
                padding: EdgeInsets.only(top: 40),
                child: OutlineButton(
                    onPressed: onTap,
                    borderSide: BorderSide(color: Color(0xFF00A0FF)),
                    child: Padding(
                      padding: EdgeInsets.only(left: 20, right: 20),
                      child: Text(
                        "Try again",
                        style: TextStyle(color: Color(0xFF00A0FF), fontSize: 12),
                      ),
                    )),
              )),
        ],
      ),
//      child: Column(
//        mainAxisAlignment: MainAxisAlignment.center,
//        crossAxisAlignment: CrossAxisAlignment.center,
//        children: <Widget>[
////          Expanded(
////            flex: 1,
////            child: SizedBox(),
////          ),
//
////          Expanded(
////            flex: 1,
////            child: SizedBox(),
////          ),
//        ],
//      ),
    ));
  }

  ///状态文字
  String _getStatusText() {
    if (statusText != null) {
      return statusText;
    }

    if (status == null) {
      return "No data";
    }

    return status == EmptyStatus.CONNECT
        ? "No data"
        : status == EmptyStatus.LOADING ? "Loading..." : "No Connection";
  }

  ///状态详情文字
  String _getStatusDetailText() {
    if (statusDetailText != null) {
      return statusDetailText;
    }
    if (status == null) {
      return "";
    }

    return status == EmptyStatus.DISCONNECT
        ? "Please check your Internet connection and try again"
        : "";
  }
}
